﻿using MxWeiXinPF.Common;
using MxWeiXinPF.WeiXinComm;
using Senparc.Weixin.MP.AdvancedAPIs;
using Senparc.Weixin.MP.AdvancedAPIs.QrCode;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace MxWeiXinPF.Web.shop
{
    public partial class userqrcode : MxWeiXinPF.Web.UI.UserPage
    {
        private string _openid = "";
        private int _wid = 0;
        public string carturl = "";
        private WeiXinCRMComm cpp = new WeiXinCRMComm();
        private int id = 0;
        public string indexurl = "";
        public string url = "";
        public string userinfourl = "";
        public string categoryurl = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                MxWeiXinPF.BLL.wx_dt_user_BaseData data = new MxWeiXinPF.BLL.wx_dt_user_BaseData();
                this._wid = MyCommFun.RequestInt("wid");
                base.wid = MyCommFun.RequestInt("wid");
                this._openid = MyCommFun.RequestOpenid();
                base.openid = MyCommFun.RequestOpenid();
                this.shopid = MyCommFun.RequestInt("shopid");
                this.indexurl = string.Concat(new object[] { "index.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.carturl = string.Concat(new object[] { "cart.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.userinfourl = string.Concat(new object[] { "userinfo.aspx?wid=", base.wid, "&openid=", this.openid, "&shopid=", shopid });
                this.categoryurl = MyCommFun.urlAddOpenid("/shop/category.aspx?wid=" + base.wid + "&shopid=" + shopid, base.openid);
                MxWeiXinPF.Model.users modelByOpenID = new MxWeiXinPF.Model.users();
                modelByOpenID = data.GetModelByOpenID(this._openid);
                if (modelByOpenID != null)
                {
                    this.id = modelByOpenID.id;
                }
                if (this.id > 0)
                {
                    string str = @"Upload\users\";
                    string path = string.Concat(new object[] { HttpRuntime.AppDomainAppPath, str, this._wid, @"\", this.id, @"\" });
                    string str3 = path + "Qrcode.jpg";
                    if (!System.IO.File.Exists(str3))
                    {
                        string error = "";
                        //string accessTokenOrAppId = this.cpp.getAccessToken(this._wid, out error);
                        if (error != "")
                        {
                            this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"getAccessToken出错，请联系管理员为您生成二维码！\");</script>");
                        }
                        else
                        {
                            CreateQrCodeResult result;
                            if (!Directory.Exists(path))
                            {
                                Directory.CreateDirectory(path);
                                result = new CreateQrCodeResult();
                                SaveQrCodeImage(QrCodeApi.GetShowQrCodeUrl(QrCodeApi.Create(null, 0, this.id, 0x2710).ticket), str3);
                                this.img.ImageUrl = string.Concat(new object[] { "../upload/users/", this._wid, "/", this.id, "/Qrcode.jpg" });
                            }
                            else if (!System.IO.File.Exists(str3))
                            {
                                result = new CreateQrCodeResult();
                                SaveQrCodeImage(QrCodeApi.GetShowQrCodeUrl(QrCodeApi.Create(null, 0, this.id, 0x2710).ticket), str3);
                                this.img.ImageUrl = string.Concat(new object[] { "../upload/users/", this._wid, "/", this.id, "/Qrcode.jpg" });
                            }
                            else
                            {
                                this.img.ImageUrl = string.Concat(new object[] { "../upload/users/", this._wid, "/", this.id, "/Qrcode.jpg" });
                            }
                        }
                    }
                    else
                    {
                        this.img.ImageUrl = string.Concat(new object[] { "../upload/users/", this._wid, "/", this.id, "/Qrcode.jpg" });
                    }
                }
                else
                {
                    this.Page.ClientScript.RegisterStartupScript(this.Page.ClientScript.GetType(), "myscripts", "<script>alert(\"您还没有关注我们的公众号，不能查看自己的二维码！\");</script>");
                }

            }
            catch (Exception ex)
            {

            }

        }

        public static void SaveQrCodeImage(string qrcodeurl, string imgPath)
        {
            if (qrcodeurl == null)
            {
                throw new ArgumentNullException("qrcodeurl");
            }
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(qrcodeurl);
            request.Method = "GET";
            using (request.GetResponse())
            {
                HttpWebResponse response = (HttpWebResponse)request.GetResponse();
                string address = response.ResponseUri.ToString();
                WebClient client = new WebClient();
                try
                {
                    client.DownloadFile(address, imgPath);
                }
                catch (Exception)
                {
                    throw new Exception("获取二维码图片失败！");
                }
            }
        }
    }
}